package com.common.client.map;

import java.util.ArrayList;

import org.gwtopenmaps.openlayers.client.Map;
import org.gwtopenmaps.openlayers.client.MapOptions;
import org.gwtopenmaps.openlayers.client.MapWidget;
import org.gwtopenmaps.openlayers.client.control.Navigation;
import org.gwtopenmaps.openlayers.client.event.EventHandler;
import org.gwtopenmaps.openlayers.client.event.EventObject;
import org.gwtopenmaps.openlayers.client.event.EventType;
import org.gwtopenmaps.openlayers.client.layer.Layer;
import org.gwtopenmaps.openlayers.client.util.JSObject;

import com.common.shared.map.GisMap;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.Slider;
import com.smartgwt.client.widgets.events.ValueChangedEvent;
import com.smartgwt.client.widgets.events.ValueChangedHandler;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.toolbar.ToolStrip;

public class MapViewerPanel extends VLayout {

	private MapPanel mapPanel;

	private Slider hSlider;
	private ToolStrip toolStrip;
	private Navigation navigator;

	private ArrayList<GisMapEventHandler> gisMapEventHandlers = new ArrayList<GisMapEventHandler>();

	public MapViewerPanel(GisMap gisMap, boolean savecookies) {
		super();
		// this.setWidth("70%");
		mapPanel = new MapPanel(gisMap, this, true,savecookies);
		mapPanel.setWidth("100%");
		mapPanel.setHeight("100%");

		// mapPanel.setTop(33);
		// mapPanel.setBorder("1px solid #808080");

		// getMap().getEvents().register("moveend", getMap(), );
		HLayout hl = new HLayout();
		hl.setWidth100();

		// mapPanel.setBorder("1px solid red");
		hl.setAutoHeight();
		// hl.setBorder("1px solid #808080");
		hl.setTop(0);
		toolStrip = new ToolStrip();
		toolStrip.setWidth100();
		toolStrip.setHeight(24);
		hl.addMember(toolStrip);
		this.addMember(hl);
		try {
			this.addMember(mapPanel);
		} catch (Exception e) {
			// TODO: handle exception
		}

	}

	public static native JSObject createRegularPolygon(JSObject point,
			double radius, int sides, double rotation) /*-{
		return OpenLayers.Geometry.Polygon.createRegularPolygon(point, radius,
				sides, rotation);
	}-*/;

	public MapOptions getMapOptions() {
		return mapPanel.getMapOptions();
	}

	void mapCreationComplete() {
		hSlider = new Slider();
		int zoomLevels = getZoomLevels() - 1;
		hSlider.setTitle("");
		hSlider.setMinValue(1);
		hSlider.setMaxValue(zoomLevels);
		hSlider.setNumValues(zoomLevels);
		hSlider.setTop(20);
		hSlider.setLeft(0);
		hSlider.setValue(getMap().getZoom());
		hSlider.setHeight(300);
		hSlider.addValueChangedHandler(new ValueChangedHandler() {
			@Override
			public void onValueChanged(ValueChangedEvent event) {
				if (getMap().getZoom() != event.getValue())
					getMap().zoomTo((int) event.getValue());
			}
		});

		EventHandler eh = new EventHandler() {
			@Override
			public void onHandle(EventObject eventObject) {
				onHandleMapEvent(eventObject);
			}
		};
		this.addChild(hSlider);
		navigator = new Navigation();
		getMap().addControl(navigator);
		Map map = getMap();
		map.getEvents().register(EventType.MAP_MOVEEND, map, eh);
		map.getEvents().register("mouseup", map, eh);
		// GisMapEvent.registerAllEvents(eh, getMap());
		for (GisMapEventHandler gisMapEventHandler : gisMapEventHandlers) {
			gisMapEventHandler.creationComplete();
		}
	}

	public void setNavigatorEnabled(boolean enabled) {
		if (hSlider != null) {
			if (enabled) {
				navigator.activate();
			} else {
				navigator.deactivate();
			}
			hSlider.setDisabled(!enabled);
		}
	}

	public Slider gethSlider() {
		return hSlider;
	}

	public ToolStrip getToolStrip() {
		return toolStrip;
	}

	public void addGisMapEventHandler(GisMapEventHandler gisMapEventHandler) {
		gisMapEventHandlers.add(gisMapEventHandler);
	}

	public void removeGisMapEventHandler(GisMapEventHandler gisMapEventHandler) {
		gisMapEventHandlers.remove(gisMapEventHandler);
	}

	public void onHandleMapEvent(EventObject eventObject) {
		Map map = getMap();
		GisMapEvent gisMapEvent = new GisMapEvent(eventObject, map);
		if (gisMapEvent.getEvent_type() == GisMapEvent.MAP_MOVEEND) {
			try {
				hSlider.setValue(getMap().getZoom());
			} catch (Exception e) {
				// TODO: handle exception
			}

		}
		for (GisMapEventHandler gisMapEventHandler : gisMapEventHandlers) {
			gisMapEventHandler.onHandle(gisMapEvent);
		}
	}

	private int getZoomLevels() {
		Layer[] leyrs = getMap().getLayers();
		for (int i = 0; i < leyrs.length; i++) {
			if (leyrs[i].isBaseLayer()) {
				return leyrs[i].getJSObject().getPropertyAsInt("numZoomLevels");
			}
		}
		return getMap().getJSObject().getPropertyAsInt("numZoomLevels");
	}

	public Layer getBaseLayer() {
		Layer[] leyrs = getMap().getLayers();
		for (int i = 0; i < leyrs.length; i++) {
			if (leyrs[i].isBaseLayer()) {
				return leyrs[i];
			}
		}
		return null;
	}

	public Map getMap() {
		return mapPanel.getMap();
	}

	public int getSrid(String srtSrid) {
		return Integer
				.parseInt(srtSrid.substring(srtSrid.indexOf((char) ':') + 1));
	}

	public int getSrid() {
		return getSrid(getMap().getProjection());
	}

	private Canvas getViewPanel() {
		ToolStrip toolStrip = new ToolStrip();
		toolStrip.setWidth100();
		toolStrip.setHeight(24);

		return toolStrip;
	}

	public MapWidget getMapWidget() {
		return mapPanel.getMapWidget();
	}
}
